package com.example.learndevice.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.CompoundButton
import com.example.learndevice.R
import com.example.learndevice.databinding.ActivityTurnTextureBinding
import com.example.mydefinedview1.base.BaseActivity

class TurnTextureActivity : BaseActivity<ActivityTurnTextureBinding>(),CompoundButton.OnCheckedChangeListener {

    private val list = listOf<String>("0.0", "0.2", "0.4", "0.6", "0.8", "1.0")

    inner class MySelectedListener:AdapterView.OnItemSelectedListener{
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            //设置透明度
            mBinding.ttvCircle.alpha = list[position].toFloat()
        }

        override fun onNothingSelected(parent: AdapterView<*>?) {

        }
    }

    override fun ActivityTurnTextureBinding.initBinding() {
        mBinding.ttvCircle.surfaceTextureListener = ttvCircle
        ckControl.setOnCheckedChangeListener(this@TurnTextureActivity)
        initSpinner()
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        if (buttonView?.id == mBinding.ckControl.id){
            if (isChecked){
                mBinding.ckControl.text = "停止"
                mBinding.ttvCircle.start()
            }else{
                mBinding.ckControl.text = "转动"
                mBinding.ttvCircle.stop()
            }
        }
    }

    private fun initSpinner(){
        val adapter = ArrayAdapter<String>(this,R.layout.item_select,list)
        mBinding.spAlpha.prompt = "请选择透明度"
        mBinding.spAlpha.adapter = adapter
        mBinding.spAlpha.setSelection(1)
        mBinding.spAlpha.onItemSelectedListener = MySelectedListener()

    }
}